//
//  PackFileDown.m
//  etionLib
//
//  Created by JingXing Wu on 12-5-12.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "PackFileDown.h"

//#import "FschunksObj.h"

#import "XLGContext.h"

//#define PACKLEN		200000

@implementation CPackFileDown

@synthesize m_delegate;

-(int)Connect
{
//	[self InitGWService:[XLGContext context].m_szPip port:[XLGContext context].m_nPport];
//	return [m_service InitStream];
    return 0;
}

-(void)Close
{
//	[m_service CloseStream];
//	[self ReleaseGWService];
}

//-(void)Start:(CFsfilesObj*)fsfile
//{
    /*
    NSAutoreleasePool* pool=[NSAutoreleasePool new];
    
    int nResult=FALSE;
    NSError* error=nil;
    
    nResult=[self Connect];
    if(nResult!=YES)
    {
        [m_delegate FileDownloadError:nResult error:error];
        goto FUNEXIT;
    }
    
    m_fsfile=[fsfile retain_mm];
    m_bDownlaod=YES;
    
    nResult=[m_service GetFsFilesLengthSSOSend:[XLGContext context].m_sessionid enterprisenumber:fsfile.m_nEnterprisenumber fsfiles:fsfile];
    if(nResult==YES)
    {
        int nLength=0;
        nResult=[m_service GetFsFilesLengthSSORecv:&nLength protocolerror:&error];
        [error autorelease_mm];
        
#ifdef DEBUG
        if(nLength==0)
            NSLog(@"文件获取长度错误,%d,%d",nResult,error.code);
#endif
        
        if(nLength==0)
            nResult=FALSE;
        
        if(nResult==YES&&error==nil)
        {
            int nLen=[m_delegate FileLength:nLength];
            
#ifdef DEBUG
            NSLog(@"文件长度:%d",nLength);
#endif
            
            void(^DownloadFsFilesSSO)(int nLen)=^(int nLen)
            {
                int nNum=0;
                
                while(m_bDownlaod)
                {
                    NSAutoreleasePool* pool=[NSAutoreleasePool new];
                    
                    int nS=nLen;
                    int nL=MIN(nLen+PACKLEN,nLength-1);
                    NSString* range=[NSString stringWithFormat:@"%d-%d",nS,nL];
                    
                    CFschunksObj* chunk=[[CFschunksObj new]autorelease_mm];
                    chunk.m_szFileid=fsfile.m_szFileid;
                    chunk.m_szFilename=fsfile.m_szFilename;
                    chunk.m_nChunknum=nNum++;
                    chunk.m_szChunkrange=range;
                    
#ifdef DEBUG
                    NSLog(@"请求第%d分段",nNum);
#endif
                    
                    int nResult=[m_service DownloadFsFilesSSOSend:[XLGContext context].m_sessionid enterptisenumber:fsfile.m_nEnterprisenumber fsfiles:fsfile fschunks:chunk];
                    if(nResult==YES)
                    {
                        CFschunksObj* c=nil;
                        NSError* error=nil;
                        nResult=[m_service DownloadFsFilesSSORecv:&c protocolerror:&error];
                        [c autorelease_mm];
                        [error autorelease_mm];
                        
#ifdef DEBUG
                        if(nResult==YES&&c.m_data.length>0)
                            NSLog(@"请求第%d分段请求成功",nNum);
                        else
                            NSLog(@"请求第%d分段请求失败",nNum);
#endif
                        
                        if(nResult==YES&&c.m_data.length>0)
                        {
                            [m_delegate FileDownloadPack:c.m_data];
                            nLen+=c.m_data.length;
                            
                            if(nLen==nLength)
                            {
#ifdef DEBUG
                                NSLog(@"文件下载完成");
#endif
                                
                                [m_delegate FileDownloadFinish];
                                m_bDownlaod=NO;
                            }
                        }
                        else
                        {
                            if(nResult!=TERMINATEREADWRITE)
                                [m_delegate FileDownloadError:nResult error:error];
                            m_bDownlaod=NO;
                        }
                    }
                    else
                    {
                        if(nResult!=TERMINATEREADWRITE)
                            [m_delegate FileDownloadError:nResult error:error];
                        m_bDownlaod=NO;
                    }
                    
                    [pool release_mm];
                } 
            };
            
            if(nLen==0)
            {
                DownloadFsFilesSSO(nLen);
            }else
            {
                int nDiff=nLength-nLen;
                if(nDiff>0)
                {
                    DownloadFsFilesSSO(nLen);
                }
                else if(nDiff==0)
                {
                    [m_delegate FileDownloadFinish];
                    goto FUNEXIT;
                }
                else
                {
                    if(nResult!=TERMINATEREADWRITE)
                        [m_delegate FileDownloadError:nResult error:error];
                    goto FUNEXIT;
                }
            }
        }
        else
        {
            if(nResult!=TERMINATEREADWRITE)
                [m_delegate FileDownloadError:nResult error:error];
            goto FUNEXIT;
        }
    }
    
FUNEXIT:
    [pool release_mm];
     */
//}

-(void)Cancel
{
//    m_bDownlaod=NO;
//    [self Close];
}

-(void)dealloc
{
//    m_bDownlaod=NO;
//    [m_fsfile release_mm];
    [super dealloc];
}

@end
